<template>
  <div class="com-container">
    <div class="title" :style="comStyle">
      <span>{{ showTitle }}</span>
      <span class="iconfont icon-arrow-down-bold" :style="comStyle"  @click="showChoice = !showChoice">&#xe6eb;</span>
      <div class="select-con" v-show="showChoice">
        <div class="select-item" v-for="item in selectTypes()" :key="item.key" @click="handleSelect(item.key)">
          {{ item.text }}
        </div>
      </div>
    </div>
    <div class="com-chart" ref="trend_ref"></div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      chartInstance: null,
      allData: null,
      month: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
      type: [{
        key: 0,
        text: '地区销售趋势'
      }, {
        key: 1,
        text: '商品销售趋势'
      }, {
        key: 2,
        text: '商家销量趋势'
      }],
      showChoice: false, // 是否显示可选项
      choiceType: 0, // 显示的数据类型
      titleFontSize: 0 // 指明标题的字体大小
    }
  },
  created () {
    // 当socket来数据的时候，会调用getData这个函数
    this.$socket.registerCallBack('trendData', this.getData)
  },
  mounted () {
    this.initChart()
    // this.getData()
    // 发送信息给服务器，告诉服务器，现在需要数据
    this.$socket.send({
      action: 'getData',
      socketType: 'trendData',
      chartName: 'trend'
    })
    window.addEventListener('resize', this.screenAdapter)
    // 刚初始化时屏幕适配
    this.screenAdapter()
  },
  destroyed () {
    window.removeEventListener('resize', this.screenAdapter)
    this.$socket.unRegisterCallBack('trendData')
  },
  computed: {
    // 设置给标题的样式
    comStyle () {
      return {
        fontSize: this.titleFontSize + 'px'
      }
    },
    showTitle () {
      // return null
      // allData是要从后端取过来的数据，需要先判断一下
      if (!this.allData) {
        return ''
      } else {
        return this.allData[this.choiceType].title
      }
    }
  },
  methods: {
    initChart () {
      this.chartInstance = this.$echarts.init(this.$refs.trend_ref, 'chalk')
      const initOption = {
        grid: {
          left: '3%',
          top: '35%',
          right: '4%',
          bottom: '1%',
          containLabel: true
        },
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          left: 20,
          top: '15%',
          icon: 'circle'
        },
        xAxis: {
          type: 'category',
          boundaryGap: false
        },
        yAxis: {
          type: 'value'
        }
      }
      this.chartInstance.setOption(initOption)
    },
    async getData (ret) {
      // 获取服务器的数据，对this.allData进行赋值之后，调用updateChart方法更新图标
      // const { data: res } = await this.$http.get('trend')
      // console.log(res.data)
      // this.allData = res.data
      this.allData = ret
      this.updateChart()
    },
    updateChart () {
      // 半透明的颜色值
      const colorArr1 = [
        'rgba(11, 168, 44, 0.5)',
        'rgba(44, 110, 255, 0.5)',
        'rgba(22, 242, 217, 0.5)',
        'rgba(254, 33, 30, 0.5)',
        'rgba(250, 105, 0, 0.5)'
      ]
      // 全透明的颜色值
      const colorArr2 = [
        'rgba(11, 168, 44, 0)',
        'rgba(44, 110, 255, 0)',
        'rgba(22, 242, 217, 0)',
        'rgba(254, 33, 30, 0)',
        'rgba(250, 105, 0, 0)'
      ]
      // x 轴数据
      const timeArr = this.month
      // y 轴的数据,暂时先取出map这个节点的数据
      // map 代表地区销量趋势
      // seller代表商家销量趋势
      // commodity代表商品销量趋势
      // 处理图标需要的数据
      const valueArr = this.allData[this.choiceType].data
      // 图标数据，一个图标中显示5条折线图
      const seriesArr = valueArr.map((item, index) => {
        return {
          type: 'line', // 折线图
          name: item.name,
          data: item.data,
          // 设置为堆叠图
          stack: 'map',
          areaStyle: {
            color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
              {
                offset: 0,
                color: colorArr1[index]
              }, // %0的颜色值
              {
                offset: 1,
                color: colorArr2[index]
              } // 100%的颜色值
            ])
          }
        }
      })
      // 图例的数据
      const legendArr = valueArr.map(item => {
        return item.name
      })
      const dataOption = {
        xAxis: {
          data: timeArr
        },
        legend: {
          data: legendArr
        },
        series: seriesArr
      }
      this.chartInstance.setOption(dataOption)
    },
    screenAdapter () {
      this.titleFontSize = this.$refs.trend_ref.offsetWidth / 100 * 3.6
      const adapterOption = {
        legend: {
          itemWidth: this.titleFontSize,
          itemHeight: this.titleFontSize,
          itemGap: this.titleFontSize,
          textStyle: {
            fontSize: this.titleFontSize / 2
          }
        }
      }
      this.chartInstance.setOption(adapterOption)
      this.chartInstance.resize()
    },
    selectTypes () {
      return this.type.filter(item => {
        return item.key !== this.choiceType
      })
    },
    handleSelect (currentType) {
      this.choiceType = currentType
      // 确定完种类后，开始过滤展示剩下的种类
      this.selectTypes()
      this.updateChart()
      this.showChoice = false
    }
  }
}
</script>

<style lang="less" scoped>
.title {
  position: absolute;
  left: 20px;
  top: 20px;
  z-index: 10;
  color: white;
  .title-icon {
    margin-left: 10px;
    cursor: pointer;
  }
  .select-con {
    background-color: #222733;
  }
}
</style>
